const app = getApp();
const serviceUrl = getApp().globalData.serviceUrl;
const loginbehavior = require('loginbehavior');
const util = require('../../utils/util.js');
Component({
	behaviors: [loginbehavior],
	/**
	 * 页面的初始数据
	 */
	data: {
		disabled: true,
		no: '',
		pwd: '',
		isRememberPwd: app.globalData.isRememberPwd,
		show: false,
		selectData: [],
		index: 0,
		optWidth: 0
	},
	methods: {

		//输入改变事件
		inputChange: function(e) {
			var targetname = e.currentTarget.dataset.name;
			this.setData({
				[targetname]: e.detail.value
			});
			if (this.data.no && this.data.pwd) {
				this.setData({
					disabled: false
				});
			}
		},


		/**
		 * 生命周期函数--监听页面加载
		 */
		onLoad: async function(options) {
      
			let that = this;
			console.log(options)
			if (typeof this.getTabBar === 'function' &&
				this.getTabBar()) {
				this.getTabBar().setData({
					showTabBaer: false
				})
			}
			//如果是本地环境就自动登录
			if (serviceUrl.toLowerCase().indexOf("http://localhost") != -1) {
				this.setData({
					no: 'AIERCHINA\\sc51',
					pwd: 'aier.123456',
				});
				//await this.formSubmit(null);
			} else {

			}

			wx.getStorage({
				key: 'hisLog',
				success: function(res) {
					that.setData({
						selectData: res.data
					});
				}
			});

		},

		/**
		 * 生命周期函数--监听页面显示
		 */
		onShow: function() {
			var that = this;
			setTimeout(function() {
				wx.createSelectorQuery().selectAll('.input-item').boundingClientRect(function(rect) {
					//console.log(rect[0].width)
					that.setData({
						optWidth: rect[0].width
					});
				}).exec()
			}, 500);
			var userpwd = wx.getStorageSync("userpwd");
			if (userpwd) {
				this.setData({
					no: userpwd.user,
					pwd: userpwd.pwd ? userpwd.pwd : "",
					isRememberPwd: true
				});
				app.globalData.isRememberPwd = true;
			}
			if (this.data.no == '' || this.data.pwd == '') {
				this.setData({
					disabled: true
				});
			} else {
				this.setData({
					disabled: false
				});
			}
		},

		selectOntap() {
			console.log("ontap");
			this.setData({
				show: !this.data.show
			});
		},
		// 点击下拉列表
		optionTap(e) {
			let Index = e.currentTarget.dataset.index; //获取点击的下拉列表的下标
			let list = this.data.selectData;
			this.setData({
				index: Index,
				show: !this.data.show,
				no: list[Index].no,
				pwd: list[Index].pwd
			});
		},

		formSubmit: async function(e) {
			if (!this.data.no || !this.data.pwd) {
				var sm = await this.showModalSync('提示', '用户名密码为空');

				return;
			}

			wx.showLoading({
				title: '登录中...',
			});

			try {
				//debugger;
				var result = await this.LoginPwd();
				var logindata = result.data;

				if (logindata.error) {
					wx.hideLoading();
					var sm = await this.showModalSync('提示', '登陆失败，请检查用户名和密码');
					this.setData({
						disabled: false
					});

				} else {
					//debugger;
					await this.setUserStorage(result.data.access_token);

					let mobilerole = app.globalData.userInfo.mobilerole_systemuser
					let roleIds = [1, 2, 3, 4, 5, 7, 12, 13];
					if (!util.haveRole(mobilerole, roleIds)) {
						wx.hideLoading();
						var sm = await this.showModalSync('提示', '你没有移动权限，请联系管理员');
						this.setData({
							disabled: false
						});
					} else {
						this.setData({
							disabled: true
						});
						var list = this.data.selectData;
						var result = list.some(item => {
							if (item.no == this.data.no) {
								return true;
							}
						})
						if (!result) {
							list.push({
								no: this.data.no,
								pwd: this.data.pwd
							});
						}
						wx.setStorage({
							key: 'hisLog',
							data: list
						})
						wx.switchTab({
							url: '../tasks/list/list'
						});

						// wx.redirectTo({
						// 	url: '../screens/result/add/add',
						// });

						// wx.switchTab({
						// 	url: '../screens/list/list',
						// }); 

					}
				}
			} catch (e) {
				this.setData({
					disabled: false
				});
			} finally {
				wx.hideLoading();
			}
		},

		/**
		 * 生命周期函数--监听页面初次渲染完成
		 */
		onReady: function() {

		},



		//记住密码
		rememberPwdChange: function(e) {
			this.setData({
				isRememberPwd: e.detail
			});
			app.globalData.isRememberPwd = e.detail;

			if (e.detail) {
				wx.setStorageSync('userpwd', {
					user: this.data.no,
					pwd: this.data.pwd
				});
			} else {
				wx.setStorageSync('userpwd', {
					user: this.data.no
				});
			}
		},

		/**
		 * 生命周期函数--监听页面隐藏
		 */
		onHide: function() {

		},

		/**
		 * 生命周期函数--监听页面卸载
		 */
		onUnload: function() {

		},

		/**
		 * 页面相关事件处理函数--监听用户下拉动作
		 */
		onPullDownRefresh: function() {

		},

		/**
		 * 页面上拉触底事件的处理函数
		 */
		onReachBottom: function() {

		},

		/**
		 * 用户点击右上角分享
		 */
		onShareAppMessage: function() {

		},


	}

})
